package com.clz.servise.Impl;

import com.clz.Dao.Impl.UserDaoImpl;
import com.clz.Dao.UserDao;
import com.clz.entiy.User;
import com.clz.servise.UserService;

import java.util.Collections;
import java.util.List;

public class UserServiceImpl implements UserService {
    public final static int PAGE_SIZE=3;
    private UserDao userDao = new UserDaoImpl();

    @Override
    public boolean login(String username, String password) {
        User user = userDao.selectUserByNameAndPsw(username, password);
        return user != null;

    }

    @Override
    public boolean signIn(String username, String password) {
        if (userDao.selectUserByName(username) != null) {
            return false;
        }else {
            int n=userDao.insertUser(username, password);
            return n==1;
        }
    }

    @Override
    public int selectUserCount() {
        return userDao.selectUserCount();
    }

    @Override
    public List<User> Paging(int pageIndex, int pageSize, String keyword) {
        int StartNumber=pageSize*(pageIndex-1);
        List<User> list=userDao.selectUserLimit(StartNumber,pageSize , keyword);
        return list;
    }

    @Override
    public int getTotalPages(int count, int pageSize) {
        return count%pageSize==0?count/pageSize:(count/pageSize)+1;
    }

    @Override
    public boolean deleteUser(int userId) {
        return userDao.deleteUserById(userId)==1;
    }

    @Override
    public boolean insertUser(User user) {
      return userDao.insertUser(user) ==1;
    }

}
